﻿jQuery.fn.selectdecorator = function ($options) {


    var defaults = {
        DIVClass: 'Select_Decorator_Holder'
    };

    $current = jQuery(this);
    $ID = $current.attr('id');
    jQuery.extend(defaults, $options);

    newSelect = '<div id="DivDetEstacion"> <span id="detselect"  charset="UTF-8" style="-moz-user-select: none;"></span>';

    newSelect += '<select';
    newSelect += ' name="' + $current.attr('name');
    newSelect += '" id="' + $current.attr('id');
    newSelect += '" class="' + $current.attr('class');
    newSelect += '" onchange = UI_Select("detselect","detselect");  >';
    newSelect += $current.text();
    newSelect += '</select>';
    newSelect += ' </div>';

    $decorator = jQuery(newSelect);
    $decorator.addClass(defaults['DIVClass']);

    $current.parent().append($decorator);
    $current.remove();

    $newSelect = jQuery('#' + $ID);

 

    $setFlag = 0;
    $newSelect.find('option').each(
        function () {
            if ($setFlag == 0) {
                $newSelect.parent().find('span').html(jQuery(this).html());
                $newSelect.val(jQuery(this).val());
                $setFlag = 1;
            }
        }
    );
    $newSelect.bind('change', optionChanged);
}


function optionChanged() {
    $option = jQuery(this);
    $setFlag = 0;
    $option.find('option').each(
        function () {
            if ($setFlag == 0) {
                if ($option.val() == jQuery(this).attr('value')) {
                    $option.parent().find('span').html(jQuery(this).html());
                    $setFlag = 1;
                }
            }
        }
    );
}
